package com.coderdream.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.coderdream.bean.Logging;
import com.coderdream.bean.Student;
import com.coderdream.dao.LoggingDao;
import com.coderdream.dao.StudentDao;

public class MyServlet extends HttpServlet {

	public static String TAG = "MyServlet";

	private Logger logger = Logger.getLogger(MyServlet.class);

	private LoggingDao loggingDao = new LoggingDao(this.getClass().getName());
	/**
	 * 
	 */
	private static final long serialVersionUID = 6703993436577206766L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter pw = resp.getWriter();
		pw.println("你好hello Servlet" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
		logger.debug(TAG + " ###### log4j debug"
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));

		logger.debug(TAG + " #1# processRequest");

		// SqlBean sqlBean = new SqlBean();
		// String sql1 = "select * from student order by id";
		// List<JavaBean> javaBeanList = sqlBean.getSearch(sql1);
		// pw.println(TAG + " javaBeanList size():" + javaBeanList.size());
		// for (Iterator<JavaBean> iterator = javaBeanList.iterator(); iterator
		// .hasNext();) {
		// JavaBean javaBean = iterator.next();
		// logger.debug(TAG + " JavaBean:" + javaBean);
		// pw.println(TAG + " JavaBean:" + javaBean);
		// }

		StudentDao studentDao = new StudentDao();
		String sql = "select * from student order by id";
		List<Student> studentList = studentDao.getSearch(sql);
		pw.println(TAG + " student size():" + studentList.size());
		for (Iterator<Student> iterator = studentList.iterator(); iterator.hasNext();) {
			Student student = iterator.next();
			logger.debug(TAG + " student:" + student);
			pw.println(TAG + " student:" + student);
		}
		List<Logging> loggingList = loggingDao.query();
		pw.println(TAG + " logging size():" + loggingList.size());
		for (Iterator<Logging> iterator = loggingList.iterator(); iterator.hasNext();) {
			Logging logging = iterator.next();
			logger.debug(TAG + " logging:" + logging);
			pw.println(TAG + " logging:" + logging);
		}

		boolean flag1 = logger.isDebugEnabled();
		boolean flag2 = logger.isInfoEnabled();
		boolean flag3 = logger.isTraceEnabled();
		// Timestamp logDate = new Timestamp(Calendar.getInstance().getTime().getTime());
		// Logging logging = new Logging(logDate, "###DEBUG### flag1:flag2:" + flag1 + ":" + flag2, TAG,
		// "hello Servlet doGet");
		// loggingDao.add(logging);
		loggingDao.debug("###DEBUG### DebugEnabled:InfoEnabled:TraceEnabled:" + flag1 + ":" + flag2 + ":" + flag3);

		// System.out.println("####" + new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
	}
}